package net.xrkw.server.gateway.handler;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.stereotype.Component;
import org.springframework.core.env.Environment;              // ✅
import org.springframework.core.env.ConfigurableEnvironment;  // ✅
@Component
public class EnvPrinterRunner implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(EnvPrinterRunner.class);

    @Autowired
    private Environment env;

    @Override
    public void run(ApplicationArguments args) {
        log.info("========== 最终生效的配置 ==========");
        if (env instanceof ConfigurableEnvironment) {
            ConfigurableEnvironment ce = (ConfigurableEnvironment) env;
            ce.getPropertySources().stream()
                    .filter(ps -> ps instanceof EnumerablePropertySource)
                    .map(ps -> (EnumerablePropertySource<?>) ps)
                    .forEach(ps -> {
                        for (String key : ps.getPropertyNames()) {
                            if (key.startsWith("spring.") || key.startsWith("server.")
                                    || key.startsWith("nacos.") || key.contains("profiles")) {
                                log.info("{} = {}", key, ps.getProperty(key));
                            }
                        }
                    });
        }
    }
}
